<?
/**
 * This file is part of XNAT light.
 *
 * XNAT light is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XNAT light is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XNAT light.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace NRG\Database\Mappers;

use NRG\Framework\Exceptions\IllegalArgumentException as IllegalArgumentException;
use NRG\Framework\Exceptions\QueryException as QueryException;
use NRG\Xnat as Xnat;

/**
 * XNAT project mapper
 *
 * @package NRG\Database\Mappers
 */
class Project extends Mapper
{
    /**
     * Constructor
     */
    public function __construct()
    {
	parent::__construct();
    }

    /**
     * Retrieve Project object based on accession
     *
     * @param string $id
     * @param boolean $like
     * @return NRG\Xnat\Project
     */
    public function byId($id=null)
    {
	if(!is_string($id))
	    throw new IllegalArgumentException("Project id must be a string");
	else if($id == "")
	    throw new IllegalArgumentException("Project id cannot be null");

	$projects = $this->byList(array($id));

	return current($projects);
    }

    /**
     * Get projects by list of IDs
     *
     * @param array $list (int, string)
     * @return array (string, NRG\Xnat\Project)
     */
    public function byList(array $list=null)
    {
	if($list === null)
	    throw new IllegalArgumentException("List cannot be null");

	$numItems = count($list);

	if($numItems == 0)
	    throw new IllegalArgumentException("List cannot be empty");

	/**
	 * Get general project information
	 */
	// build where clause
	$where = "id=$1";
	for($i = 2; $i <= $numItems; $i++)
	    $where .= " OR id=$" . $i;

	$query = "SELECT * FROM xnat_projectdata WHERE $where";
	$result = $this->_xnat->query($query, $list);

	if(count($result) == 0)
	    throw new IllegalArgumentException("Query returned empty result");

	$projects = array();

	foreach($result as $row)
	{
	    $id = $row["id"];

	    $projects[$id] = new Xnat\Project();
	    $projects[$id]->setID($row["id"]);
	    $projects[$id]->setName($row["name"]);
	    $projects[$id]->setDescription($row["description"]);
	}

	/**
	 * Get specific project information
	 */
	// build where clause
	$where = "id=$1";
	for($i = 2; $i <= $numItems; $i++)
	    $where .= " OR id=$" . $i;

	$query = "SELECT id AS project, archivepath AS path FROM arc_project JOIN arc_pathinfo ON " .
		"(arc_project.paths_arc_pathinfo_id=arc_pathinfo.pathinfo_info) WHERE $where";

	$result = $this->_xnat->query($query, $list);

	foreach($result as $row)
	    $projects[$row["project"]]->setArchivePath($row["path"]);

	/**
	 * Get all Subject IDs
	 */
	// build where clause
	$where = "project=$1";
	for($i = 2; $i <= $numItems; $i++)
	    $where .= " OR project=$" . $i;

	$query = "SELECT * FROM xnat_subjectdata WHERE $where";
	$result = $this->_xnat->query($query, $list);

	foreach($result as $row)
	    $projects[$row["project"]]->addSubject($row["id"]);

	return $projects;
    }

    /**
     * Get all project IDs
     *
     * @return array (NRG\Xnat\Project)
     * @throws QueryException
     */
    public function all()
    {
	$query = "SELECT id FROM xnat_projectdata";

	$result = $this->_xnat->query($query);

	if(count($result) == 0)
	    throw new QueryException(QueryException::EMPTY_RESULT, $query);

	$ids = array();

	foreach($result as $row)
	    $ids[] = $row["id"];

	return $this->byList($ids);
    }

    /**
     * Get Project label given a wildcard
     *
     * @param string $wildcard
     * @return array
     */
    public function getWildcardLabels($wildcard)
    {
	if(!is_string($wildcard))
	    throw new IllegalArgumentException("Wildcard must be a string");
	else if(trim($wildcard) == "")
	    throw new IllegalArgumentException("Wildcard cannot be empty");

	$params = array($wildcard);
	$query = "SELECT name from xnat_projectdata WHERE name LIKE $1";
	$result = $this->_xnat->query($query, $params);

	$labels = array();

	foreach($result as $row)
	    $labels[] = $row["name"];

	return $labels;
    }

    /**
     * Get Project IDs given a wildcard label
     *
     * @param string $wildcard
     * @return array
     */
    public function getWildcardIDs($wildcard)
    {
	if(!is_string($wildcard))
	    throw new IllegalArgumentException("Wildcard must be a string");

	$params = array($wildcard);
	$query = "SELECT id from xnat_projectdata WHERE id LIKE $1";
	$result = $this->_xnat->query($query, $params);

	$ids = array();

	foreach($result as $row)
	    $ids[] = $row["id"];

	return $ids;
    }
}